﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace OpiConsole
{
    public static class Parser
    {
        public static int toInt(String str)
        {
            try
            {
                return int.Parse(str);
            }
            catch (System.FormatException)
            {
                OpiConsole.Console.Singleton.Print("Couldn't parse string to int '" + str + 
                    "' using default value.", ConsoleLevel.DEBUG);
            }

            return 0;
        }

        public static float toFloat(String str)
        {
           try
            {
                return float.Parse(str);
            }
            catch (System.FormatException)
            {
                OpiConsole.Console.Singleton.Print("Couldn't parse string to float '" + str +
                    "' using default value.", ConsoleLevel.DEBUG);
            }

           return 0;
        }

        public static double toDouble(String str)
        {
            try
            {
                return double.Parse(str);
            }
            catch (System.FormatException)
            {
                OpiConsole.Console.Singleton.Print("Couldn't parse string to double '" + str +
                    "' using default value.", ConsoleLevel.DEBUG);
            }

            return 0;
        }

        public static Color toColor(String str)
        {
            int r=0, g=0, b=0, a=0;
            String tmpVal;

            tmpVal = str;

            int pos = tmpVal.IndexOf(' ');

            //Find Red
            if (pos < 0)
                return new Color(0, 0, 0, 255);
            else
            {
                r = Parser.toInt(tmpVal.Substring(0, 3));
            }

            //Find Green
            pos = tmpVal.IndexOf(' ', 0);
            if (pos < 0)
                return new Color(r, 0, 0, 255);
            else
            {
                g = Parser.toInt(tmpVal.Substring(pos + 1, 3));
            }

            //Find Blue
            pos = tmpVal.IndexOf(' ', pos + 1);
            if (pos < 0)
                return new Color(r, g, 0, 255);
            else
            {
                b = Parser.toInt(tmpVal.Substring(pos + 1, 3));
            }

            //Find Alpha
            pos = tmpVal.IndexOf(' ', pos + 1);
            if (pos < 0)
                return new Color(r, g, b, 255);
            else
            {
                a = Parser.toInt(tmpVal.Substring(pos + 1, 3));
            }

            return new Color(r, g, b, a);
        }

        public static Color toColor(List<string> args, int index, bool alpha)
        {
            if (index + 2 >= args.Count)
            {
                OpiConsole.Console.Singleton.Print("Not enough parameters for toColor.", ConsoleLevel.DEBUG);
                return new Color(0, 0, 0);
            }

            int r = 0, g = 0, b = 0, a = 255;

            r = Parser.toInt(args[index]);
            g = Parser.toInt(args[index+1]);
            b = Parser.toInt(args[index+2]);
            if (alpha)
                a = Parser.toInt(args[index+3]);

            return new Color(r, g, b, a);
        }

        public static Vector2 toVector2(String str)
        {
            float x = 0, y = 0;
            String tmpVal;

            tmpVal = str;

            int pos = tmpVal.IndexOf(' ');

            //Find X
            if (pos < 0)
                return new Vector2(0, 0);
            else
            {
                x = Parser.toFloat(tmpVal.Substring(0, 3));
            }

            //Find Y
            pos = tmpVal.IndexOf(' ', 0);
            if (pos < 0)
                return new Vector2(x, 0);
            else
            {
                y = Parser.toFloat(tmpVal.Substring(pos + 1, 3));
            }

            return new Vector2(x, y);
        }

        public static Vector2 toVector2(List<string> args, int index)
        {
            if (index + 1 >= args.Count)
            {
                OpiConsole.Console.Singleton.Print("Not enough parameters for toVector2.", ConsoleLevel.DEBUG);
                return new Vector2(0, 0);
            }

            float x = 0, y = 0;

            x = Parser.toFloat(args[index]);
            y = Parser.toFloat(args[index + 1]);

            return new Vector2(x, y);
        }

        public static Vector3 toVector3(String str)
        {
            float x=0, y=0, z=0;
            String tmpVal;

            tmpVal = str;

            int pos = tmpVal.IndexOf(' ');

            //Find X
            if (pos < 0)
                return new Vector3(0, 0, 0);
            else
            {
                x = Parser.toFloat(tmpVal.Substring(0, 3));
            }

            //Find Y
            pos = tmpVal.IndexOf(' ', 0);
            if (pos < 0)
                return new Vector3(x, 0, 0);
            else
            {
                y = Parser.toFloat(tmpVal.Substring(pos + 1, 3));
            }

            //Find Z
            pos = tmpVal.IndexOf(' ', pos + 1);
            if (pos < 0)
                return new Vector3(x, y, 0);
            else
            {
                z = Parser.toFloat(tmpVal.Substring(pos + 1, 3));
            }

            return new Vector3(x, y, z);
        }

        public static Vector3 toVector3(List<string> args, int index)
        {
            if (index + 2 >= args.Count)
            {
                OpiConsole.Console.Singleton.Print("Not enough parameters for toVector3.", ConsoleLevel.DEBUG);
                return new Vector3(0, 0, 0);
            }

            float x = 0, y = 0, z = 0;

            x = Parser.toFloat(args[index]);
            y = Parser.toFloat(args[index + 1]);
            z = Parser.toFloat(args[index + 2]);

            return new Vector3(x, y, z);
        }

        public static Rectangle toRectangle(String str)
        {
            int x = 0, y = 0, w = 0, h = 0;
            String tmpVal;

            tmpVal = str;

            int pos = tmpVal.IndexOf(' ');

            //Find X
            if (pos < 0)
                return new Rectangle(0,0,0,0);
            else
            {
                x = Parser.toInt(tmpVal.Substring(0, 3));
            }

            //Find Y
            pos = tmpVal.IndexOf(' ', 0);
            if (pos < 0)
                return new Rectangle(x,0,0,0);
            else
            {
                y = Parser.toInt(tmpVal.Substring(pos + 1, 3));
            }

            //Find W
            pos = tmpVal.IndexOf(' ', pos + 1);
            if (pos < 0)
                return new Rectangle(x,y,0,0);
            else
            {
                w = Parser.toInt(tmpVal.Substring(pos + 1, 3));
            }

            //Find H
            pos = tmpVal.IndexOf(' ', pos + 1);
            if (pos < 0)
                return new Rectangle(x,y,w,0);
            else
            {
                w = Parser.toInt(tmpVal.Substring(pos + 1, 3));
            }

            return new Rectangle(x,y,w,h);
        }

        public static Rectangle toRectangle(List<string> args, int index)
        {
            if (index + 3 >= args.Count)
            {
                OpiConsole.Console.Singleton.Print("Not enough parameters for toRectangle.", ConsoleLevel.DEBUG);
                return new Rectangle(0, 0, 0, 0);
            }
            
            int x=0, y=0, w=0, h=0;

            x = Parser.toInt(args[index]);
            y = Parser.toInt(args[index + 1]);
            w = Parser.toInt(args[index + 2]);
            h = Parser.toInt(args[index + 3]);

            return new Rectangle(x,y,w,h);
        }

        public static bool toBool(String str)
        {
            try
            {
                return bool.Parse(str);
            }
            catch (System.FormatException)
            {
                OpiConsole.Console.Singleton.Print("Couldn't parse string to bool '" + str +
                    "' using default.", ConsoleLevel.DEBUG);
            }

            return false;
        }

        public static char toChar(String str)
        {
            //TODO: Check this works
            if (str.Length > 0)
                return (str.ToCharArray()[0]);
            else
                return ' ';

        }
    }
}
